package com.effectiv.crm.controller;

import javax.inject.Inject;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.effectiv.crm.domain.Contact;

import com.effectiv.crm.service.api.ContactService;
import com.effectiv.crm.service.api.PickListService;

@Slf4j
@Controller
@RequestMapping("/contact")
public class ContactController {

	@Inject
	private ContactService service;

	@Inject
	private PickListService pickListService;
	

	@RequestMapping("/list")
	public String list(Model model) {
		model.addAttribute(service.findByDeleted(false));
		return "listcontacts";
	}

	@RequestMapping(value={"/edit","/new","/details"})
	public String editOrNew(Model model,@RequestParam(value="id", required=false) String id
			,@RequestParam(value="view", required=false, defaultValue="false") boolean view
			) {
		Contact contact = null;
		
		boolean edit = false;
		boolean newContact = false;
		
		if(StringUtils.isEmpty(id)) {
			contact = BeanUtils.instantiate(Contact.class);
			newContact = true;
			
		}
		else{
			contact = service.findOne(id);
			
			//handle missing lead
			if(contact == null){
				//TODO - Return error and move back to list page
				contact = BeanUtils.instantiate(Contact.class);
				
			}
			
			
			if(!view){
				edit = true;
			}
		}
		

		model.addAttribute(contact);
		
		model.addAttribute("salutationList", pickListService.findByDomain("Salutation"));
		model.addAttribute("view", view);
		model.addAttribute("edit", edit);
		model.addAttribute("newContact", newContact);
		model.addAttribute("id", id);
		
		return "newcontact";
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(@Valid Contact contact,
			BindingResult result, Model model) {
		
		log.info("Contact submitted for saving - {}", contact);
		
		if (result.hasErrors()) {
			return "newcontact";
		}
		else{
			service.update(contact);
			model.addAttribute("message",
					"Lead updated successfully.");
		}
		
		
		model.addAttribute(contact);
		
		model.addAttribute("salutationList", pickListService.findByDomain("Salutation"));
		model.addAttribute("view", false);
		model.addAttribute("edit", true);
		model.addAttribute("newContact", false);
		model.addAttribute("id", contact.getId());
		
		return "newcontact";
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
	public String delete(@RequestParam("id") String id,
			final RedirectAttributes redirectAttributes){
		Contact contact = new Contact();
		contact.setId(id);
		
		service.delete(contact);
		
		redirectAttributes.addFlashAttribute("message", "Contact deleted successfully.");
		
		return "redirect:list";
		
	}
	
	@RequestMapping("/copy")
	public String copy(Model model,@RequestParam(value="id") String id) {
		Contact contact = service.findOne(id);
		if(contact != null )
			contact.setId(null);

		model.addAttribute(contact);
		
		model.addAttribute("salutationList", pickListService.findByDomain("Salutation"));
		model.addAttribute("view", false);
		model.addAttribute("edit", false);
		model.addAttribute("newContact", true);
		model.addAttribute("id", null);
		
		return "newcontact";
	}

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(@Valid Contact contact,
			BindingResult result, Model model) {
		
		
		if (result.hasErrors()) {
			log.info("Result - {}",result);
			return "newlead";
		}
		else{
			
			log.info("Saving contact");
			contact.setId(null);
			service.save(contact);
			model.addAttribute("message",
					"Contact saved successfully.");
		}

		
		model.addAttribute(contact);
		model.addAttribute("salutationList", pickListService.findByDomain("Salutation"));
		model.addAttribute("view", true);
		model.addAttribute("edit", false);
		model.addAttribute("newContact", false);
		model.addAttribute("id", contact.getId());
		
		
		return "newcontact";
	}
}
